<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog class="ba-operate-dialog" :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)" @close="baTable.toggleForm" width="50%">
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div class="ba-operate-form" :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '' : 'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'">
                <el-form v-if="!baTable.form.loading" ref="formRef" @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)" :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'" :rules="rules">
                    <!-- <FormItem :label="t('redemptionCode.code')" type="string" v-model="baTable.form.items!.code"
                        prop="code" :placeholder="t('Please input field', { field: t('redemptionCode.code') })" /> -->
                    <FormItem :label="t('redemptionCode.title')" type="string" v-model="baTable.form.items!.title"
                        prop="title" :placeholder="t('Please input field', { field: t('redemptionCode.title') })" />
                    <FormItem :label="t('redemptionCode.description')" type="string"
                        v-model="baTable.form.items!.description" prop="description"
                        :placeholder="t('Please input field', { field: t('redemptionCode.description') })" />
                    <!-- <FormItem :label="t('redemptionCode.type')" type="radio" v-model="baTable.form.items!.type"
                        prop="type"
                        :input-attr="{ content: { single: t('redemptionCode.type single'), multiple: t('redemptionCode.type multiple') } }"
                        :placeholder="t('Please select field', { field: t('redemptionCode.type') })" /> -->
                    <FormItem :label="t('redemptionCode.max_uses')" type="number" v-model="baTable.form.items!.max_uses"
                        prop="max_uses" :input-attr="{ step: 1 }"
                        :placeholder="t('Please input field', { field: t('redemptionCode.max_uses') })" />
                    <!-- <FormItem :label="t('redemptionCode.used_count')" type="number"
                        v-model="baTable.form.items!.used_count" prop="used_count" :input-attr="{ step: 1 }"
                        :placeholder="t('Please input field', { field: t('redemptionCode.used_count') })" /> -->
                    <FormItem :label="t('redemptionCode.start_time')" type="datetime"
                        v-model="baTable.form.items!.start_time" prop="start_time"
                        :placeholder="t('Please select field', { field: t('redemptionCode.start_time') })" />
                    <FormItem :label="t('redemptionCode.end_time')" type="datetime" v-model="baTable.form.items!.end_time"
                        prop="end_time"
                        :placeholder="t('Please select field', { field: t('redemptionCode.end_time') })" />
                    <FormItem :label="t('redemptionCode.status')" type="radio" v-model="baTable.form.items!.status"
                        prop="status"
                        :input-attr="{ content: { '0': t('redemptionCode.status 0'), '1': t('redemptionCode.status 1'), '2': t('redemptionCode.status 2') } }"
                        :placeholder="t('Please select field', { field: t('redemptionCode.status') })" />
                    <FormItem :label="t('redemptionCode.remark')" type="textarea" v-model="baTable.form.items!.remark"
                        prop="remark" :input-attr="{ rows: 3 }" @keyup.enter.stop=""
                        @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                        :placeholder="t('Please input field', { field: t('redemptionCode.remark') })" />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)"
                    type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') :
                    t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    max_uses: [buildValidatorData({ name: 'number', title: t('redemptionCode.max_uses') })],
    used_count: [buildValidatorData({ name: 'number', title: t('redemptionCode.used_count') })],
    discount_amount: [buildValidatorData({ name: 'number', title: t('redemptionCode.discount_amount') })],
    discount_percent: [buildValidatorData({ name: 'number', title: t('redemptionCode.discount_percent') })],
    start_time: [buildValidatorData({ name: 'date', title: t('redemptionCode.start_time') })],
    end_time: [
        buildValidatorData({ name: 'date', title: t('redemptionCode.end_time') }),
        {
            validator: (rule: any, value: any, callback: Function) => {
                if (value && baTable.form.items?.start_time && new Date(value) <= new Date(baTable.form.items.start_time)) {
                    callback(new Error('结束时间必须晚于开始时间'))
                } else {
                    callback()
                }
            },
            trigger: 'change'
        }
    ],
    create_time: [buildValidatorData({ name: 'date', title: t('redemptionCode.create_time') })],
    update_time: [buildValidatorData({ name: 'date', title: t('redemptionCode.update_time') })],
})
</script>

<style scoped lang="scss"></style>
